﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Protocol.equipment;

namespace Pixysoft.Weblications.Sango.Command.equipment
{
    partial class SangoEquipmentHandler
    {
        public eqm_vipbuy eqm_vipbuy(long eqmserialcode)
        {
            string userCode = SangoManager.GetUsercode();

            eqm_vipbuy response = ReflectionManager.CreatePojo<eqm_vipbuy>();
            response.Succeed = false;

            Entities.Eqm_Equipment eqm = EqmEquipmentHelper.searchBySerialcode(eqmserialcode);
            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Property property = UsrPropertyDao.getProperty(userCode);

            if (eqm.Visiblelevel > profile.Viplevel)
            {
                response.Message = "VIP等级不足";
                return response;
            }

            if (property.Usedrepositorycount >= property.Repositorycount)
            {
                response.Message = "仓库已满，无法购买";
                return response;
            }

            long goldprice = eqm.Equipmentprice;

            if (!UsrProfileDao.tryDecreaseGoldValue(profile, goldprice))
            {
                response.Message = "金币不足";
                return response;
            }

            Entities.Usr_Equipment myeqm = EquipmentDao.createUserEqm(profile, eqm);

            UsrPropertyDao.enterRepository(property);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                command.Insert(myeqm);

                command.Update(profile);

                command.Update(property);

                command.Commit();
            });

            response.usereqm = EquipmentFacade.ToEquipment(myeqm, new List<Pixysoft.Weblications.Sango.Entities.Usr_Equipmentextra>());
            response.profile = UsrProfileFacade.ToProfile(profile);
            response.property = UsrProfileFacade.ToProperty(property);
            response.Succeed = true;
            return response;
        }


    }
    public interface eqm_vipbuy : ISangoResponse
    {
        player_profile profile { get;set;}
        player_property property { get;set;}
        usr_equipment usereqm { get;set;}
    }
}
